﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ZombieGame
{
    public class GUIText : GUIItem
    {
        public TextAlignment Alignment
        {
            get { return _alignment; }
            set
            {
                _alignment = value;
                v1();
            }
        }
        private TextAlignment _alignment;
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                v1();
            }
        }
        private string _text;
        
        public float LineSpacing;
        string[] lines;
        Vector2[] subs;
        public GUIText(string text) :
            base()
        {
            Text = text;
            LineSpacing = 2f;
        }

        public override void Draw(SpriteBatch batch)
        {
            for (int i = 0; i < lines.Length; i++)
                batch.DrawString(ZombieGame.Font1, lines[i], Position + new Vector2(0f, i * (Size.Y / (float)lines.Length - (float)ZombieGame.Font1.LineSpacing * lines.Length) + LineSpacing * i), Color, Rotation, subs[i], Scale, SpriteEffects.None, 0f);
        }

        private void v1()
        {
            if (_text != null)
            {
                Size = ZombieGame.Font1.MeasureString(_text);
                if ((int)Alignment % 3 == 1)
                    Origin.X = Size.X / 2f;
                if ((int)Alignment % 3 == 2)
                    Origin.X = Size.X;
                if ((int)Alignment > 2)
                    Origin.Y = Size.Y / 2f;
                if ((int)Alignment > 5)
                    Origin.Y = Size.Y;
                lines = _text.Split('\n');
                subs = new Vector2[lines.Length];
                for (int i = 0; i < lines.Length; i++)
                {
                    Vector2 m = ZombieGame.Font1.MeasureString(lines[i]);
                    if ((int)Alignment % 3 == 1)
                        subs[i].X = m.X / 2f;
                    if ((int)Alignment % 3 == 2)
                        subs[i].X = m.X;
                }
            }
        }

        public override float Layer
        {
            get { return 0f; }
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }

    public enum TextAlignment
    {
        CenterLeft = 3,
        CenterCenter = 4,
        CenterRight = 5,
        TopLeft = 0,
        TopCenter = 1,
        TopRight = 2,
        BottomLeft = 6,
        BottomCenter = 7,
        BottomRight = 8
    }
}
